﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Data;

public partial class System_Pages_Navigation_QuickLink : SiteFox.Base.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            this.LoadQuickLinks();
        }
    }

    #region ControlsEvents
    //----- Panel QuickLinks AddOrEdit -----//
    protected void imb_QuickLinks_Add_Click(object sender, ImageClickEventArgs e)
    {
        pan_Background.Visible = true;
        pan_QuickLinks_AddOrEdit.Visible = true;
        lab_QuickLinks_AddOrEdit_Title.Text = Resources.dcCommon.dc_Add;
        lib_QuickLinks_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Add;
        hf_QuickLinks_SelectedId.Value = "";
        pan_QuickLink_Title.Visible = false;
        gv_QuickLink.DataSource = null;
        gv_QuickLink.DataBind();
    }
    protected void lib_QuickLinks_AddOrEdit_Close_Click(object sender, EventArgs e) 
    {
        this.ResetQuickLinksAddOrEdit();
    }
    protected void lib_QuickLinks_AddOrEdit_Ok_Click(object sender, EventArgs e) 
    {
        SiteFox.Business.Model.QuickLinks _QuickLinks = new SiteFox.Business.Model.QuickLinks();
        if (hf_QuickLinks_SelectedId.Value != "")
        {
            _QuickLinks = SiteFox.Business.Manager.QuickLinks.GetByID(Convert.ToInt32(hf_QuickLinks_SelectedId.Value));
        }
        _QuickLinks.Active = ucc_QuickLinks_AddOrEdit_Active.Value;
        _QuickLinks.Description = ucc_QuickLinks_AddOrEdit_Description.Value;
        _QuickLinks.Name = ucc_QuickLinks_AddOrEdit_Name.Value;

        if (hf_QuickLinks_SelectedId.Value == "")
        {
            _QuickLinks.Created = DateTime.Now;
            _QuickLinks.CreatedBy = this.CurrentUser().Username;
            SiteFox.Business.Manager.QuickLinks.Create(_QuickLinks);
        }
        else
        {
            _QuickLinks.Modified = DateTime.Now;
            _QuickLinks.ModifiedBy = this.CurrentUser().Username;
            SiteFox.Business.Manager.QuickLinks.Edit(_QuickLinks);
        }
        this.ResetQuickLinksAddOrEdit();
        this.LoadQuickLinks();
    }

    //----- Panel QuickLinks Delete -----//
    protected void lib_QuickLinks_Delete_Close_Click(object sender, EventArgs e)
    {
        this.ResetQuickLinksDelete();
    }
    protected void lib_QuickLinks_Delete_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Manager.QuickLinks.Delete(Convert.ToInt32(hf_QuickLinks_SelectedId.Value));
        this.ResetQuickLinksDelete();
        this.LoadQuickLinks();
        pan_QuickLink_Title.Visible = false;
        hf_QuickLinks_SelectedId.Value = "";
        gv_QuickLink.DataSource = null;
        gv_QuickLink.DataBind();

    }

    //----- QuickLinks GridView ActionButtons -----//
    protected void lib_QuickLinks_Details_Click(object sender, EventArgs e)
    {
        hf_QuickLinks_SelectedId.Value = ((LinkButton)(sender)).CommandArgument;
        this.LoadQuickLink(Convert.ToInt32(hf_QuickLinks_SelectedId.Value));
    }
    protected void lib_QuickLinks_Edit_Click(object sender, EventArgs e)
    {
        hf_QuickLinks_SelectedId.Value = ((LinkButton)(sender)).CommandArgument;
        int _ID = Convert.ToInt32(hf_QuickLinks_SelectedId.Value);
        var _QuickLinks = SiteFox.Business.Manager.QuickLinks.GetByID(_ID);
        pan_Background.Visible = true;
        pan_QuickLinks_AddOrEdit.Visible = true;
        lab_QuickLinks_AddOrEdit_Title.Text = Resources.dcCommon.dc_Edit;
        lib_QuickLinks_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Edit;
        ucc_QuickLinks_AddOrEdit_Active.Value = _QuickLinks.Active;
        ucc_QuickLinks_AddOrEdit_Description.Value = _QuickLinks.Description;
        ucc_QuickLinks_AddOrEdit_Name.Value = _QuickLinks.Name;
    }
    protected void lib_QuickLinks_Delete_Click(object sender, EventArgs e)
    {
        hf_QuickLinks_SelectedId.Value = ((LinkButton)(sender)).CommandArgument;
        int _ID = Convert.ToInt32(hf_QuickLinks_SelectedId.Value);
        pan_Background.Visible = true;
        pan_QuickLinks_Delete.Visible = true;
    }

    //----- Panel QuickLink AddOrEdit -----//
    protected void imb_QuickLink_Add_Click(object sender, ImageClickEventArgs e)
    {
        pan_Background.Visible = true;
        pan_QuickLink_AddOrEdit.Visible = true;
        lab_QuickLink_AddOrEdit_Title.Text = Resources.dcCommon.dc_Add;
        lib_QuickLink_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Add;
    }
    protected void lib_QuickLink_AddOrEdit_Close_Click(object sender, EventArgs e)
    {
        this.ResetQuickLinkAddOrEdit();
    }
    protected void lib_QuickLink_AddOrEdit_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Model.QuickLink _QuickLink = new SiteFox.Business.Model.QuickLink();
        if (hf_QuickLink_AddOrEdit_Id.Value != "")
        {
            _QuickLink = SiteFox.Business.Manager.QuickLink.GetByID(Convert.ToInt32(hf_QuickLink_AddOrEdit_Id.Value));
        }
        _QuickLink.Description = ucc_QuickLink_AddOrEdit_Description.Value;
        _QuickLink.Target = ddl_QuickLink_AddOrEdit_Target_Value.SelectedValue;
        _QuickLink.Title = ucc_QuickLink_AddOrEdit_Title.Value;
        _QuickLink.Url = ucc_QuickLink_AddOrEdit_Url.Value;

        if (hf_QuickLink_AddOrEdit_Id.Value == "")
        {
            var _QuickLinks = SiteFox.Business.Manager.QuickLink.GetByQuickLinks(Convert.ToInt32(hf_QuickLinks_SelectedId.Value));
            _QuickLink.Ordering = _QuickLinks.Count;
            _QuickLink.ID_QuickLinks = Convert.ToInt32(hf_QuickLinks_SelectedId.Value);
            SiteFox.Business.Manager.QuickLink.Create(_QuickLink);
        }
        else
        {
            SiteFox.Business.Manager.QuickLink.Edit(_QuickLink);
        }
        this.ResetQuickLinkAddOrEdit();
        this.LoadQuickLink(Convert.ToInt32(hf_QuickLinks_SelectedId.Value));
    }

    //----- Panel QuickLink Delete -----//
    protected void lib_QuickLink_Delete_Close_Click(object sender, EventArgs e)
    {
        this.ResetQuickLinkDelete();
    }
    protected void lib_QuickLink_Delete_Ok_Click(object sender, EventArgs e)
    {
        int _ID = Convert.ToInt32(hf_QuickLink_Delete_Id.Value);
        SiteFox.Business.Manager.QuickLink.Delete(_ID);
        this.ResetQuickLinkDelete();
        this.LoadQuickLink(Convert.ToInt32(hf_QuickLinks_SelectedId.Value));
        this.QuickLinksModified();
    }

    //----- QuickLink GridView ActionButtons -----//
    protected void lib_QuickLink_MoveUp_Click(object sender, EventArgs e)
    {
        int _ID = Convert.ToInt32(((LinkButton)(sender)).CommandArgument);
        var _QuickLink = SiteFox.Business.Manager.QuickLink.GetByID(_ID);
        _QuickLink.Ordering++;
        SiteFox.Business.Manager.QuickLink.Edit(_QuickLink);
        this.LoadQuickLink(Convert.ToInt32(hf_QuickLinks_SelectedId.Value));
        this.QuickLinksModified();
    }
    protected void lib_QuickLink_MoveDown_Click(object sender, EventArgs e)
    {
        int _ID = Convert.ToInt32(((LinkButton)(sender)).CommandArgument);
        var _QuickLink = SiteFox.Business.Manager.QuickLink.GetByID(_ID);
        _QuickLink.Ordering--;
        SiteFox.Business.Manager.QuickLink.Edit(_QuickLink);
        this.LoadQuickLink(Convert.ToInt32(hf_QuickLinks_SelectedId.Value));
        this.QuickLinksModified();
    }
    protected void lib_QuickLink_Edit_Click(object sender, EventArgs e)
    {
        hf_QuickLink_AddOrEdit_Id.Value = ((LinkButton)(sender)).CommandArgument;
        int _ID = Convert.ToInt32(hf_QuickLink_AddOrEdit_Id.Value);
        var _QuickLink = SiteFox.Business.Manager.QuickLink.GetByID(_ID);
        pan_Background.Visible = true;
        pan_QuickLink_AddOrEdit.Visible = true;
        lab_QuickLink_AddOrEdit_Title.Text = Resources.dcCommon.dc_Edit;
        lib_QuickLink_AddOrEdit_Ok.Text = Resources.dcCommon.dc_Edit;
        ucc_QuickLink_AddOrEdit_Description.Value = _QuickLink.Description;
        ucc_QuickLink_AddOrEdit_Title.Value = _QuickLink.Title;
        ucc_QuickLink_AddOrEdit_Url.Value = _QuickLink.Url;
        ddl_QuickLink_AddOrEdit_Target_Value.SelectedValue = _QuickLink.Target;
    }
    protected void lib_QuickLink_Delete_Click(object sender, EventArgs e)
    {
        hf_QuickLink_Delete_Id.Value = ((LinkButton)(sender)).CommandArgument;
        int _ID = Convert.ToInt32(hf_QuickLink_Delete_Id.Value);
        pan_Background.Visible = true;
        pan_QuickLink_Delete.Visible = true;
    }
    #endregion

    #region Methods
    void LoadQuickLinks() 
    {
        gv_QuickLinks.DataSource = SiteFox.Business.Manager.QuickLinks.GetAll();
        gv_QuickLinks.DataBind();
    }
    void LoadQuickLink(int _QuickLinksId) 
    {
        pan_QuickLink_Title.Visible = true;
        var _QuickLinks = SiteFox.Business.Manager.QuickLinks.GetByID(_QuickLinksId);
        lab_QuickLink_Title.Text = Resources.dcCommon.dc_DetailsFor + " <strong>" + _QuickLinks.Name + "</strong>";
        gv_QuickLink.DataSource = SiteFox.Business.Manager.QuickLink.GetByQuickLinks(_QuickLinksId);
        gv_QuickLink.DataBind();
    }
    void ResetQuickLinksAddOrEdit() 
    {
        pan_Background.Visible = false;
        pan_QuickLinks_AddOrEdit.Visible = false;
        lab_QuickLinks_AddOrEdit_Title.Text = "";
        lib_QuickLinks_AddOrEdit_Ok.Text = "";
        hf_QuickLinks_SelectedId.Value = "";
        ucc_QuickLinks_AddOrEdit_Active.Value = false;
        ucc_QuickLinks_AddOrEdit_Description.Value = "";
        ucc_QuickLinks_AddOrEdit_Name.Value = "";
    }
    void ResetQuickLinksDelete() 
    {
        pan_Background.Visible = false;
        pan_QuickLinks_Delete.Visible = false;
        hf_QuickLinks_SelectedId.Value = "";
    }
    void ResetQuickLinkAddOrEdit() 
    {
        pan_Background.Visible = false;
        pan_QuickLink_AddOrEdit.Visible = false;
        lab_QuickLink_AddOrEdit_Title.Text = "";
        lib_QuickLink_AddOrEdit_Ok.Text = "";
        hf_QuickLink_AddOrEdit_Id.Value = "";
        ucc_QuickLink_AddOrEdit_Description.Value = "";
        ucc_QuickLink_AddOrEdit_Title.Value = "";
        ucc_QuickLink_AddOrEdit_Url.Value = "";
        ddl_QuickLink_AddOrEdit_Target_Value.SelectedValue = "_self";
    }
    void ResetQuickLinkDelete() 
    {
        pan_Background.Visible = false;
        pan_QuickLink_Delete.Visible = false;
        hf_QuickLink_Delete_Id.Value = "";
    }
    void QuickLinksModified() 
    {
        var _QuickLinks = SiteFox.Business.Manager.QuickLinks.GetByID(Convert.ToInt32(hf_QuickLinks_SelectedId.Value));
        _QuickLinks.Modified = DateTime.Now;
        _QuickLinks.ModifiedBy = Page.User.Identity.Name;
        SiteFox.Business.Manager.QuickLinks.Edit(_QuickLinks);
        this.LoadQuickLinks();
    }
    #endregion
}